package sky.springboot.literature_and_art.exception;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import sky.springboot.literature_and_art.pojo.Result;

//创建集中处理异常容器
@RestControllerAdvice
public class GlobalExceptionHandler {

    //确认异常类型
    @ExceptionHandler(Exception.class)
    public Result handleException(Exception e) {
        e.printStackTrace();
        //使用StringUtils.hasLength判断是否有这个错误
        return Result.error(StringUtils.hasLength(e.getMessage())? e.getMessage() : "操作失败");
    }
}
